#include "OptionsState.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "Game.h"
#include "MainMenuState.h"
#include "BitmapFont.h"
#include <string>
using std::string;
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "MenuManager.h"
#include <fstream>
#include <sstream>
using namespace std;

COptionsState* COptionsState::GetInstance(void)
{
	static COptionsState s_Instance;
	return &s_Instance;
}

COptionsState::COptionsState(void)
{
	m_nBackgroundMusID = -1;
	choice = 0;
	m_pD3D = nullptr;
	m_pDI  = nullptr;
	m_pBF = nullptr;
	m_nPointerImgID = -1;
	m_nSFXID = -1;
}

COptionsState::~COptionsState(void)
{

}

void COptionsState::Enter( void )
{
	m_pD3D = CSGD_Direct3D::GetInstance();
	m_pDI  = CSGD_DirectInput::GetInstance();
	m_pBF = new CBitmapFont;
	m_pBF->Initialize();
	LoadVolumes();
}

void COptionsState::Exit( void )
{
	delete m_pBF;
}

bool COptionsState::Input(void)
{

	if( m_pDI->KeyPressed( DIK_DOWN ) )
	{
		choice++;
		if( choice == 2 )
			choice = 0;
		if( choice == 0 )
		{
			//Your on the first choice
		}
	}

	if( m_pDI->KeyPressed( DIK_UP ) )
	{
		choice--;
		if( choice == -1 )
			choice = 1;
		if( choice == 0 )
		{
			//First
		}
	}

	
	if( m_pDI->KeyPressed( DIK_LEFT ) && choice == 0 )
	{
		volume = volume - 0.05f;
		if( volume < 0 )
			volume = 0;
		CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(volume);
	}


	if( m_pDI->KeyPressed( DIK_RIGHT ) && choice == 0 )
	{
		volume = volume + 0.05f;
		if( volume > 1 )
			volume = 1;
		CSGD_XAudio2::GetInstance()->MusicSetMasterVolume(volume);
	}

	if( m_pDI->KeyPressed( DIK_LEFT ) && choice == 1 )
	{
		SFX = SFX - 0.05f;
		if( SFX < 0 )
			SFX = 0;
		CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(SFX);

	}


	if( m_pDI->KeyPressed( DIK_RIGHT ) && choice == 1 )
	{
		SFX = SFX + 0.05f;
		if( SFX > 1 )
			SFX = 1;
		CSGD_XAudio2::GetInstance()->SFXSetMasterVolume(SFX);

	}

	if( m_pDI->KeyPressed( DIK_ESCAPE ))
	{
		CMenuManager::GetInstance()->GoBack();
		CGame::GetInstance()->ChangeState( CMenuManager::GetInstance()->PlayBack() );
			SaveVolumes();

		//return false;
	}


	return true;
}

void COptionsState::Update(float fElapsedTime)
{

}

void COptionsState::Render(void)
{

	string title;
	title = "OPTIONS:";
	m_pBF->Print(title.c_str(), 100, 60, 0.5);

	ostringstream word;
	word << "VOLUME: " << (int)(volume*100);
	m_pBF->Print(word.str().c_str(), 100, 60, 0.5);

	ostringstream word2;

	word2 << "SFX: " << (int)(SFX*100);
	m_pBF->Print(word2.str().c_str(), 100, 100, 0.5);

	CSGD_TextureManager::GetInstance()->Draw( CMenuManager::GetInstance()->GetImageID(), 70, 60+(choice*40));
	
}

void COptionsState::SaveVolumes()
{
	ofstream out("music.txt");
	if (out.is_open())
	{
		out << volume;
	}

	ofstream fout("sfx.txt");
	if (fout.is_open())
	{
		fout << SFX;
	}
	fout.close();
}

void COptionsState::LoadVolumes()
{
	ifstream fin;
	fin.open("music.txt");
	if( fin.is_open() )
	{
		fin >> volume;
	}
	fin.close();
	fin.open("sfx.txt");
	if( fin.is_open() )
	{
		fin >> SFX;
	}
}